{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[1.2, 1.5, 1.8], \n",
    "[1.3, 1.4, 1.9], \n",
    "[1.1, 1.6, 1.7]]) \n",
    "y = np.array([5, 10, 9]).T "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[37.2, 37.599999999999994, 36.8]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#循环\n",
    "a = [[1.2, 1.5, 1.8], \n",
    "    [1.3, 1.4, 1.9], \n",
    "    [1.1, 1.6, 1.7]]\n",
    "b = [5, 10, 9]\n",
    "c = []\n",
    "for i in range(len(a)):\n",
    "    tmp = 0.0\n",
    "    for j in range(len(a[i])):\n",
    "        #c.append(a[i][j]*b[j])\n",
    "        tmp =tmp+ a[i][j]*b[j]\n",
    "    c.append(tmp)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用矩阵 \n",
    "s = np.dot(X,y)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.39 µs ± 189 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#速度对比\n",
    "# 使用%time 只测一次，时间太短；\n",
    "%%timeit \n",
    "s = np.dot(X,y)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.4 µs ± 2.63 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "a = [[1.2, 1.5, 1.8], \n",
    "    [1.3, 1.4, 1.9], \n",
    "    [1.1, 1.6, 1.7]]\n",
    "b = [5, 10, 9]\n",
    "c = []\n",
    "for i in range(len(a)):\n",
    "    tmp = 0.0\n",
    "    for j in range(len(a[i])):\n",
    "        #c.append(a[i][j]*b[j])\n",
    "        tmp =tmp+ a[i][j]*b[j]\n",
    "    c.append(tmp)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
